package com.gdty.sqliteapp.activity;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.gdty.sqlite.utils.DbManger;
import com.gdty.sqlite.vo.SQLVo;
import com.gdty.sqlite.vo.UpdateVo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestActivity extends AppCompatActivity {

    public static final String CONFIG_PATH = Environment.getExternalStorageDirectory().getPath()+"/com.gdty.sqlite";
    private Context mContext;
    private DbManger db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!checkPermissions(NEEDED_PERMISSIONS)) {
                ActivityCompat.requestPermissions(this, NEEDED_PERMISSIONS, ACTION_REQUEST_PERMISSIONS);
                return;
            }
        }
        init();
    }

    public void init(){
        File path = new File(CONFIG_PATH);
        if (!path.exists()) {
            try {
                //按照指定的路径创建文件夹
                path.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        File file=new File(CONFIG_PATH+"/config.properties");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        SQLVo vo = new SQLVo();
        vo.setInitSql("create table test(id integer primary key autoincrement,name varchar(64));");
        UpdateVo u = new UpdateVo();
        u.setVersion(2);
        u.setSql("create table test2(id integer primary key autoincrement,name varchar(64));");
        List<UpdateVo> sl = new ArrayList<>();
        sl.add(u);
        vo.setUpdateSql(sl);
        db = new DbManger(mContext,CONFIG_PATH+"/sqlite.db",2,vo);
        db.execSQL("insert into test(name) values('tim')");
        String s = db.queryStr("test",null,null,null);
        System.out.println(s);
    }

    /**************************************权限start***************************************************/
    /**
     * 检查权限
     * @param neededPermissions
     * @return
     */
    protected boolean checkPermissions(String[] neededPermissions) {
        if (neededPermissions == null || neededPermissions.length == 0) {
            return true;
        }
        boolean allGranted = true;
        for (String neededPermission : neededPermissions) {
            allGranted &= ContextCompat.checkSelfPermission(this, neededPermission) == PackageManager.PERMISSION_GRANTED;
        }
        return allGranted;
    }

    protected static final int ACTION_REQUEST_PERMISSIONS = 0x001;
    protected static final String[] NEEDED_PERMISSIONS = new String[]{
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_NETWORK_STATE
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        switch (requestCode) {
            case ACTION_REQUEST_PERMISSIONS: {
                init();
            }
        }

    }
    /**************************************权限end***************************************************/
}



